<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\AuthMenu;
use Illuminate\Http\Request;

class AuthMenuController extends Controller
{
    public function index(Request $request)
    {
        $model = AuthMenu::query();
        $model = $model->with('child');
        $model = $model->where('parent_menu_id', 0);
        $result = $model->orderBy('display_order', 'asc')->get();
        return showMsg('操作成功', 200, $result);
    }

    //权限设置
    public function setting(Request $request)
    {
        $model = AuthMenu::query();
        $model = $model->with('child');
        $model = $model->where('status', 1);
        $model = $model->where('parent_menu_id', 0);
        $result = $model->orderBy('display_order', 'asc')->get();
        return showMsg('操作成功', 200, $result);
    }

    public function detail(Request $request)
    {
        $id = $request->get('menu_id');
        $result = AuthMenu::where('menu_id', $id)->first();
        if (!$result) {
            return showMsg('信息已删除', 201, $result);
        }
        return showMsg('操作成功', 200, $result);
    }

    public function beforeSave(Request $request)
    {
        $id = $request->get('menu_id');
        $data = $request->except(['menu_id', 'created_at', 'updated_at']);
        if ($id) {
            AuthMenu::where('menu_id', $id)->update($data);
            return showMsg('更新成功', 200);
        } else {
            AuthMenu::create($data);
            return showMsg('添加成功', 200);
        }
    }

    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return showMsg('请选择操作项', 201);
        }
        if (is_array($ids)) {
            foreach ($ids as $v) {
                AuthMenu::destroy('id', $v);
            }
        } else {
            AuthMenu::destroy('id', $ids);
        }
        return showMsg('删除成功', 200);
    }

    public function setStatus(Request $request)
    {
        $status = $request->get('status');
        $ids = $request->get('menu_id');
        if ($status) {
            AuthMenu::where('menu_id', $ids)->update(['status' => 1]);
        } else {
            AuthMenu::where('menu_id', $ids)->update(['status' => 0]);
        }
        return showMsg('操作成功', 200);
    }

}